package com.Sdebruijn.curriculumvitae.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.asynctask.DownloadWorkDataAsyncTask;
import com.Sdebruijn.curriculumvitae.asynctask.LoadWorkDataAsyncTask;
import com.Sdebruijn.curriculumvitae.models.Experience;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.Sdebruijn.curriculumvitae.view.RemoteImageView;
import com.actionbarsherlock.app.SherlockFragment;

/**
 * Fragment showing the applicant's work experience
 * 
 * @author Stefan de Bruijn
 * 
 */
public class WorkDataFragment extends SherlockFragment {

	/**
	 * Key used to fetch the Work object list from the Bundle savedInstanceState
	 */
	private static final String KEY_WORK = "com.Sdebruijn.cirriculumvitae.EducationFragment.work";

	/**
	 * List of Work object is kept in order to maintain our state
	 */
	public static List<Experience> mWork;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_experience, null);

		// If we have a former state we restore it, else (down)load the data
		if (savedInstanceState != null) {
			refreshFromInstanceState(view, savedInstanceState);
		} else {
			DownloadWorkDataAsyncTask mDownloadWorkDataAsyncTask = new DownloadWorkDataAsyncTask(
					view, this.getActivity(), false);
			mDownloadWorkDataAsyncTask.execute();
		}

		return view;
	}

	/**
	 * Reloads all data after this Fragment gets restored, from the Bundle
	 * savedInstanceState
	 * 
	 * @param view
	 *            The fragment's general view
	 * @param savedInstanceState
	 *            Bundle with our saved former state
	 */
	private void refreshFromInstanceState(View view, Bundle savedInstanceState) {

		// Fetch our Work list from the Bundle
		mWork = savedInstanceState.getParcelableArrayList(KEY_WORK);

		// If re-marshaling is successful, load the Work data
		if (mWork != null && !mWork.isEmpty()) {

			// Initiate the views
			LinearLayout ll_experience_data = (LinearLayout) view
					.findViewById(R.id.ll_experience_data);
			ProgressWheel pg = (ProgressWheel) view.findViewById(R.id.pw_spinner);

			LayoutInflater inflater = (LayoutInflater) this.getActivity().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);

			// Add a 'card' for each Work object by inflating it into the root
			// view
			for (Experience experience : mWork) {

				View vExperienceBlock = inflater.inflate(R.layout.merge_experience_block_shaded,
						null);

				RemoteImageView riv_experience_block_logo = (RemoteImageView) vExperienceBlock
						.findViewById(R.id.riv_experience_block_logo);
				TextView tv_experience_block_name = (TextView) vExperienceBlock
						.findViewById(R.id.tv_experience_block_name);
				TextView tv_experience_block_description = (TextView) vExperienceBlock
						.findViewById(R.id.tv_experience_block_description);

				// Fill the views with data again
				riv_experience_block_logo.setRemotePath(experience.institution.logo);
				riv_experience_block_logo.loadImage(true);

				tv_experience_block_name.setText(experience.institution.name_short);
				tv_experience_block_description.setText(experience.institution.description);

				// Add the inflated 'card' to the root view
				ll_experience_data.addView(vExperienceBlock);

				// Switch visibility of our LinearLayout holding all the data
				// and the loading-indicator
				ViewUtils.switchVisibleState(ll_experience_data, ViewUtils.State.VISIBLE, pg,
						ViewUtils.State.INVISIBLE);
			}
			// If re-marshaling fails, load Work data from the database
		} else {
			LoadWorkDataAsyncTask mLoadWorkDataAsyncTask = new LoadWorkDataAsyncTask(view,
					this.getActivity(), false, null);
			mLoadWorkDataAsyncTask.execute();
		}
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		// Save our Work objects so we can restore them later
		savedInstanceState
				.putParcelableArrayList(KEY_WORK, (ArrayList<? extends Parcelable>) mWork);
	}
}
